/**********************************************************************************************************************
*  COPYRIGHT
*  -------------------------------------------------------------------------------------------------------------------
*  Copyright (c) 2017 by Vector Informatik GmbH.                                                  All rights reserved.
*
*                This software is copyright protected and proprietary to Vector Informatik GmbH.
*                Vector Informatik GmbH grants to you only those rights as set out in the license conditions.
*                All other rights remain with Vector Informatik GmbH.
*  -------------------------------------------------------------------------------------------------------------------
*  FILE DESCRIPTION
*  -------------------------------------------------------------------------------------------------------------------
*  File       :  MemService_AsrNvM.plugin
*  Module     :  Elisa
*  Description:  Checks for AsrNvM
*  -------------------------------------------------------------------------------------------------------------------
*  AUTHOR IDENTITY
*  -------------------------------------------------------------------------------------------------------------------
*  Name                          Initials      Company
*  -------------------------------------------------------------------------------------------------------------------
*  Ondrovic Tomas                viroto        Vector Informatik GmbH
*  -------------------------------------------------------------------------------------------------------------------
*  REVISION HISTORY
*  -------------------------------------------------------------------------------------------------------------------
*  Version    Date          Author   Change Id        Description
*  -------------------------------------------------------------------------------------------------------------------
*  01.00.00   2016-03-03    viroto                    Initial creation for MemService_AsrNvM
*  01.01.00   2017-02-08    viroto                    ESCAN00093734 added CheckMultiBlockPreconditions to ensure fulfilled multi block preconditions
**********************************************************************************************************************/

/**********************************************************************************************************************
* Mandatory Functions
**********************************************************************************************************************/

/**********************************************************************************************************************
* Name         : RegisterPlugin
* Return value : Reference to a structure which contains the registration information about the plugin
* Description  : Elisa_core calls this function to query necessary information about the plugin.
*                This function is mandatory.
**********************************************************************************************************************/
def RegisterPlugin()
{
  var reg = ModulePluginRegistration()
  reg.SetVersion(0x010100)
  reg.SetPackageName("MemService_AsrNvM")
  reg.SetInputFiles(["NvM_Cfg.c"])
  return reg 
}

/**********************************************************************************************************************
* Name         : CheckVersions
* Return value : -
* Description  : Elisa_core calls this function to allow the plugin a version check against the BSW sources.
**********************************************************************************************************************/
def CheckVersions()
{
	/* Intentionally left empty. */
}

/**********************************************************************************************************************
* Name         : main
* Parameter    : None
* Return value : None
* Description  : This is the entry point of the MSSV plugin. Main calls all rule functions to check the configuration.
*                This function is mandatory.
* Requirements : ADAPT: Enter here the references to the corresponding requirements
**********************************************************************************************************************/
def main()
{
  InvokeRule("Check QM inactive", CheckQMDefines);
  InvokeRule("Check SelectForReadAll and SelectForWriteAll preconditions", CheckMultiBlockPreconditions);
}

/**********************************************************************************************************************
* Rules
**********************************************************************************************************************/

/**********************************************************************************************************************
* Name         : CheckQMDefines
* Parameter    : None
* Return value : None
* Description  : This rule checks the setting of QM-related preprocessor defines.
*                Typically it checks that QM features are inactive in safety context.
* Requirements : N/A
**********************************************************************************************************************/
def CheckQMDefines()
{
  /* function not usable for NvM because the value of NVM_DEV_ERROR_DETECT == (STD_ON) */
  /* AssertDefineIsStdOn("NVM_DEV_ERROR_DETECT") */
  /* check define version directly */
  AssertDefineEquals("NVM_DEV_ERROR_DETECT", "(STD_ON)")
}

/**********************************************************************************************************************
* Name         : CheckMultiBlockPreconditions
* Parameter    : None
* Return value : None
* Description  : This rule checks the configuration for NvM blocks with enabled SelectBlockForReadAll or
* 				 SelectBlockForWriteAll - those block has to have a RAM pointer or explicit synchronization configured.
* Requirements : N/A
**********************************************************************************************************************/
def CheckMultiBlockPreconditions()
{
	/* get block array */
	var allBlockCfgs = Array("NvM_BlockDescriptorTable_at");
	/* get define values */
	var selectForReadAllOnDef = Define("NVM_SELECT_BLOCK_FOR_READALL_ON");
	var selectForReadAllOnDefVal = selectForReadAllOnDef.GetValueAsNumber();
	print("SelectForReadAllOnDef value: " + selectForReadAllOnDefVal.to_string());
	var selectForWriteAllOnDef = Define("NVM_SELECT_BLOCK_FOR_WRITEALL_ON");
	var selectForWriteAllOnDefVal = selectForWriteAllOnDef.GetValueAsNumber();
	print("SelectForWriteAllOnDef value: " + selectForWriteAllOnDefVal.to_string());
	/* loop over each user block and the configuration block to check the preconditions */
	for (var i = 1; i < allBlockCfgs.GetSize (); ++i)
	{
		print("Executing checks for block: " + i.to_string());
		var blockCfg = allBlockCfgs.GetElemAsStruct(i);  
		
		Assert(blockCfg.HasMember("Flags_u8"));
		var blockFlags = blockCfg.GetMemberAsNumber("Flags_u8");
		print("Flags value: " + blockFlags.to_string());
		
		var blockSelectedForReadAll = ((blockFlags & selectForReadAllOnDefVal) == selectForReadAllOnDefVal);
		print("SelectForReadAll enabled: " + blockSelectedForReadAll.to_string());
		var blockSelectedForWriteAll = ((blockFlags & selectForWriteAllOnDefVal) == selectForWriteAllOnDefVal);
		print("SelectForWriteAll enabled: " + blockSelectedForWriteAll.to_string());
		
		/* SelectForReadAll or SelectForWriteAll enabled? */
		if(blockSelectedForReadAll || blockSelectedForWriteAll)
		{
			/* has to have RAM or Explicit Synchronization enabled */
			var hasRam = (blockCfg.GetMemberAsString("RamBlockDataAddr_t") != Define("NULL_PTR").GetValueAsString());
			var hasExplSyncGet = (blockCfg.GetMemberAsString("CbkGetMirrorFunc_pt") != Define("NULL_PTR").GetValueAsString());
			var hasExplSyncSet = (blockCfg.GetMemberAsString("CbkSetMirrorFunc_pt") != Define("NULL_PTR").GetValueAsString());
			
			Assert((hasRam || (hasExplSyncGet && hasExplSyncSet)));
		}
		print("");
		/* skip all other blocks */
	}
}


